function TreeNode(val) {
    this.val = val;
    this.left = null;
    this.right = null;
}

var levelOrder = function(root) {

    // 边界条件
    if (root == null) return []
    // 结果数组
    let result = []
    // 队列
    let queue = []
    queue.push(root)
    // 中间过渡数组
    let temp = []
    // 定义一个标志位
    let flag = true
    // 队列不为空的时候
    while (queue.length != 0) {
        // 每一行的队列
        while (queue.length != 0) {
            // let treeNode = queue.shift()
            temp.push(queue.shift())
        }
        // 定义一个子数组
        let subResult = []
        while (temp.length != 0) {
            let treeNode = temp.shift()
            subResult.push(treeNode.val)
            if (treeNode.left) queue.push(treeNode.left)
            if (treeNode.right) queue.push(treeNode.right)
        }
        if (!flag) subResult.reverse()
        flag = !flag
        result.push(subResult)
    }
    return result
};
let root = new TreeNode(3)
root.left = new TreeNode(9)
root.right = new TreeNode(20)
root.right.left = new TreeNode(15)
root.right.right = new TreeNode(7)
console.log(levelOrder(root))
